#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

int findIndex(vector<int>& inorder, int start, int end, int value) {
    for (int i = start; i <= end; i++) {
        if (inorder[i] == value) {
            return i;
        }
    }
    return -1;
}

TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder, int inStart, int inEnd, int& postIndex) {
    if (inStart > inEnd) {
        return nullptr;
    }

    int rootValue = postorder[postIndex];
    TreeNode* root = new TreeNode(rootValue);
    postIndex--;

    int inIndex = findIndex(inorder, inStart, inEnd, rootValue);

    root->right = buildTree(inorder, postorder, inIndex + 1, inEnd, postIndex);
    root->left = buildTree(inorder, postorder, inStart, inIndex - 1, postIndex);

    return root;
}

void preorderTraversal(TreeNode* root) {
    if (root == nullptr) {
        return;
    }
    cout << root->val << " ";
    preorderTraversal(root->left);
    preorderTraversal(root->right);
}

int main() {
    vector<int> inorder, postorder;
    string readed;
    int num;

    getline(cin, readed);
    stringstream readedstream1(readed);
    while (readedstream1 >> num) {
        inorder.push_back(num);
    }

    getline(cin, readed);
    stringstream readedstream2(readed);
    while (readedstream2 >> num) {
        postorder.push_back(num);
    }

    int postIndex = postorder.size() - 1;

    TreeNode* root = buildTree(inorder, postorder, 0, inorder.size() - 1, postIndex);

    // 输出先序遍历结果
    preorderTraversal(root);

    return 0;
}
